package com.ysm.smfu.timepicker;

import android.content.Context;
import android.text.TextUtils;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.ysm.smfu.R;
import com.ysm.smfu.area.ScrollerNumberPicker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * 城市Picker
 *
 * @author zd
 */
public class TimePicker extends LinearLayout {
    /**
     * 滑动控件
     */
    private ScrollerNumberPicker yearPicker;
    private ScrollerNumberPicker monthPicker;
    private ScrollerNumberPicker dayPicker;
    /**
     * 选择监听
     */
//    private OnSelectingListener onSelectingListener;
    /**
     * 刷新界面
     */
    private static final int REFRESH_VIEW = 0x001;
    /**
     * 临时日期
     */
    private int tempHourIndex = -1;
    private int temSecondsIndex = -1;
    private int tempMillionsIndex = -1;
    private Context context;
    private ArrayList<String> years = new ArrayList<>();
    private ArrayList<String> months = new ArrayList<>();
    private Map<Integer, ArrayList<String>> days = new HashMap<>();
    private String timeString;

    public TimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getTimeinfo();
    }

    public TimePicker(Context context) {
        super(context);
        this.context = context;
        getTimeinfo();
    }

    // 获取城市信息
    private void getTimeinfo() {
        // 读取时间信息
        Time time = new Time("GMT+8");
        time.setToNow();
        int year = time.year;

        for (int i = year - 100; i <= year; i++) {
            years.add(String.valueOf(i));
        }
        for (int i = 1; i <= 12; i++) {
            months.add(String.valueOf(i));
        }
        ArrayList<String> sanyi = new ArrayList<>();
        for (int i = 1; i <= 31; i++) {
            sanyi.add(String.valueOf(i));
        }
        ArrayList<String> sanshi = new ArrayList<>();
        for (int i = 1; i <= 30; i++) {
            sanshi.add(String.valueOf(i));
        }

        ArrayList<String> erba = new ArrayList<>();
        for (int i = 1; i <= 28; i++) {
            erba.add(String.valueOf(i));
        }
        ArrayList<String> erjiu = new ArrayList<>();
        for (int i = 1; i <= 29; i++) {
            erjiu.add(String.valueOf(i));
        }
        int[] sy = {1, 3, 5, 7, 8, 10, 12};
        int[] so = {4, 6, 9, 11};
//        二十九天
        days.put(20, erjiu);
//        二十八天
        days.put(21, erba);
        for (int i = 0; i < sy.length; i++) {
            days.put(sy[i], sanyi);
        }
        for (int i = 0; i < so.length; i++) {
            days.put(so[i], sanshi);
        }

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.city_picker, this);
        // 获取控件引用
        yearPicker = (ScrollerNumberPicker) findViewById(R.id.province);

        monthPicker = (ScrollerNumberPicker) findViewById(R.id.city);
        dayPicker = (ScrollerNumberPicker) findViewById(R.id.couny);

        yearPicker.setData(years);
        yearPicker.setDefault(80);
        monthPicker.setData(months);
        monthPicker.setDefault(6);
        dayPicker.setData(days.get(7));
        dayPicker.setDefault(10);
//		年份监听
        yearPicker.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

            @Override
            public void endSelect(int id, String text) {
//                选择完毕
                isProvinceSelect = true;
                if (TextUtils.isEmpty(text))
                    return;

                String selectMonth = monthPicker.getSelectedText();
                if (!TextUtils.isEmpty(selectMonth)) {
                    int selected = Integer.parseInt(selectMonth);
                    if (selected == 2 && Integer.parseInt(yearPicker.getSelectedText()) % 4 == 0) {
                        dayPicker.setData(days.get(20));
                    } else if (selected == 2 && Integer.parseInt(yearPicker.getSelectedText()) % 4 != 0) {
                        dayPicker.setData(days.get(21));
                    } else {
                        dayPicker.setData(days.get(selected));
                    }
                    dayPicker.setDefault(10);
                    tempHourIndex = id;
                }
            }

            @Override
            public void selecting(int id, String text) {
                isProvinceSelect = false;
            }
        });
//		月份监听
        monthPicker.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

            @Override
            public void endSelect(int id, String text) {
                isCitySelect = true;
                if (TextUtils.isEmpty(text))
                    return;

                String selectMonth = monthPicker.getSelectedText();
                if (!TextUtils.isEmpty(selectMonth)) {
                    int selected = Integer.parseInt(selectMonth);
                    if (selectMonth == null || selectMonth.equals(""))
                        return;
                    if (selected == 2 && Integer.parseInt(yearPicker.getSelectedText()) % 4 == 0) {
                        dayPicker.setData(days.get(20));
                    } else if (selected == 2 && Integer.parseInt(yearPicker.getSelectedText()) % 4 != 0) {
                        dayPicker.setData(days.get(21));
                    } else {
                        dayPicker.setData(days.get(selected));
                    }
                    dayPicker.setDefault(10);
                    int lastDay = Integer.valueOf(monthPicker.getListSize());
                    if (id > lastDay) {
                        monthPicker.setDefault(lastDay - 1);
                    }
                }
                temSecondsIndex = id;
            }

            @Override
            public void selecting(int id, String text) {
                isCitySelect = false;
            }
        });
//		天数监听
        dayPicker.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

            @Override
            public void endSelect(int id, String text) {
                isCounySelect = true;
                if (TextUtils.isEmpty(text))
                    return;
                if (tempMillionsIndex != id) {
                    int lastDay = Integer.valueOf(dayPicker.getListSize());
                    if (id > lastDay) {
                        dayPicker.setDefault(lastDay - 1);
                    }
                }
                tempMillionsIndex = id;
            }

            @Override
            public void selecting(int id, String text) {
                isCounySelect = false;
            }
        });
    }

    /**
     * 获取选择的城市信息
     *
     * @return
     */
    public String getCity_string() {
        timeString = yearPicker.getSelectedText() + "#"
                + monthPicker.getSelectedText() + "#" + dayPicker.getSelectedText();
        return timeString;
    }


    public String getYear() {
        return yearPicker.getSelectedText();
    }

    public String getMonth() {
        return monthPicker.getSelectedText();
    }

    public String getDay() {
        return dayPicker.getSelectedText();
    }

    /**
     * 省市县 选择完毕的判断
     */
    public boolean isProvinceSelect;
    public boolean isCitySelect;
    public boolean isCounySelect;

}
